package MTRequest;

import java.sql.ResultSet;

import MTProxyServer.MTConstant;
import MTProxyServer.MessageCentre;
import SQLManager.SQLManager;

public class MTServerSynContactList extends MTRequest{

	public MTServerSynContactList( MessageCentre aMSGCenter )
	{
		super( aMSGCenter );
		this.mRequestState = MTConstant.MT_REQ_SSYN_CTL_SEVRER_READY;
		this.mSendingToPeerRawData.add( "" );
	}
	
	protected void UpdateRequestState( int aPeerState )
	{
		if( MTConstant.MT_REQ_CSYN_CTL_SEND_PHONENUM_LAST == aPeerState ){
			this.mRequestState = MTConstant.MT_REQ_SSYN_CTL_REPLY_PHONENUM_LAST;
		}
		else if( MTConstant.MT_REQ_CSYN_CTL_SEND_PHONENUM == aPeerState){
			this.mRequestState = MTConstant.MT_REQ_SSYN_CTL_REPLY_PHONENUM;
		}
	}
	
	protected void ProcessReqest( int aPeerState, String peerRawData )
	{
		switch( aPeerState ){
			case MTConstant.MT_REQ_CSYN_CTL_SEND_USERID:{
				//Store UserID
				this.mReceivedFromPeerRawData.add( peerRawData );
				break;
			}
			case MTConstant.MT_REQ_CSYN_CTL_SEND_PHONENUM:
			case MTConstant.MT_REQ_CSYN_CTL_SEND_PHONENUM_LAST:{
				
				this.mReceivedFromPeerRawData.add( peerRawData );
				
				String userID = SearchUserID( peerRawData );				
				System.out.println( "Server UserID:" + userID );
				
				if( false == userID.matches( " " )){
					// update the Contact List on Server side;
					UpdateUserContactList( userID );
				}
				
				this.mSendingToPeerRawData.set( 0 , userID );
				
				break;
			}
		}
	}
	
	public void SendRequest()
	{
		String request = "" + MTConstant.MT_REQID_SYNCHRONIZE_CONTACT_LIST + MTConstant.MT_REQ_ITEM_SEPARATER + 
		this.mRequestState + MTConstant.MT_REQ_ITEM_SEPARATER;
		switch( this.mRequestState ){
			case MTConstant.MT_REQ_SSYN_CTL_SEVRER_READY:{
				request += " "; // Send feedback without Content
			}
			case MTConstant.MT_REQ_SSYN_CTL_REPLY_PHONENUM:
			case MTConstant.MT_REQ_SSYN_CTL_REPLY_PHONENUM_LAST:{
				request += this.mSendingToPeerRawData.get( 0 );
				break;
			}
		}
		this.SendMSG(request);		
		
		if( MTConstant.MT_REQ_CSYN_CTL_SEND_PHONENUM_LAST == this.mRequestState ){
			this.Finish();
		}
	}
	
	
	private String SearchUserID( String PhoneNum )
	{
		String userID = " ";
		SQLManager sqlManager = SQLManager.GetInstance();
		try{
			sqlManager.ConnectSQL( );
			
			ResultSet rs = sqlManager.Query( 
					String.format("SELECT * FROM " + MTConstant.MT_SQL_DB + ".UserTable where PhoneNumber = '%s'", PhoneNum )
					, MTConstant.MT_SQL_DB );
			
			if( rs.next() ){
				userID = rs.getString( "UserID" );
				
				System.out.println( "UserID: " + rs.getString( "UserID" ));
				System.out.println( "UserName: " + rs.getString( "UserName" ));
				System.out.println( "PhoneNumer: " + rs.getString( "PhoneNumber" ));
			}
			sqlManager.Close();
		}
		catch( Exception e ){
			e.printStackTrace();
		}
		
		return userID;
	}
	
	private void UpdateUserContactList( String newUserID )
	{
		String requestUserID = this.mReceivedFromPeerRawData.get( 0 );
		
		String query = String.format(
				"INSERT INTO %s.User_%s_ContactListTable ( UserID ) VALUES ( '%s' )", 
				MTConstant.MT_SQL_DB,
				requestUserID,
				newUserID );
		
		SQLManager sqlManager = SQLManager.GetInstance();
		try{
			sqlManager.ConnectSQL();
			sqlManager.UpdateTable( query );
			sqlManager.Close();
		}
		catch( Exception e ){
			e.printStackTrace();
		}
	}
	
}
